<?php

class Page extends PageModel {
	var $requests = array();
	var $assigns = array('page' => 1, 1 => 2, 2 => 3, 3 => 4);
	var $link = false;
	var $domain = '';
	var $config;

	public function __construct(&$config) {
		parent::__construct($config);
		$this->config = $config;
	}
	
	function getRequests() {
		$uri = $_SERVER['REQUEST_URI'];
		$this->requests = explode('/', $uri);
	}
	
	public function handleRequest() {
		unset($this->requests[0]);
		foreach($this->assigns as $k => $v) {
			if(isset($this->requests[$v])) {
				unset($this->requests[$k]);
				if($k == 'page' && empty($this->requests[$v])) {
					$this->requests[$v] = 'home';
				}
				$this->requests[$k] = $this->requests[$v];
			}else{
				if(isset($this->requests[$k])) {
					unset($this->requests[$k]);
				}
			}
		}
	}
	
	public function setRequests($request, $value) {
		$this->requests[$request] = $value;
	}
	
	public function getLink($page, $value1 = NULL, $value2 = NULL, $value3 = NULL) {
		$this->link = $this->domain.'/'.$page.'/';
		$this->link .= ($value1 != NULL) ? $value1.'/' : NULL;
		$this->link .= ($value2 != NULL) ? $value2.'/' : NULL;
		$this->link .= ($value3 != NULL) ? $value3.'/' : NULL;
		return $this->link;
	}
	
	public function loadFile($file, $directory = NULL) {
		$directory = ($directory == NULL) ? 'views/' : $directory;
		if(file_exists($file)) {
			require_once($directory.$file);
		}else{
			die('The included '.$file.' file cannot be found..');
		}
	}
	
	public function loadController($controller) {
		$path = 'controllers/'.$controller.'/'.$controller.'.controller.php';
		if(file_exists($path)) {
			require_once($path);
			return true;
		}
		return false;
	}
	
	public function isReq($request, $value = NULL) {
		if(isset($this->requests[$request])) {
			if($value == NULL) {
				return true;
			}
			if($value != NULL && $value == $this->requests[$request]) {
				return true;
			}
			return false;
		}
	}
}

?>